﻿namespace KIMS;

/// <summary>
/// 系统配置类。
/// </summary>
public static class AppConfig
{
    /// <summary>
    /// 取得系统ID。
    /// </summary>
    public static string AppId => "TARGET_APPID";

    /// <summary>
    /// 取得系统名称。
    /// </summary>
    public static string AppName => "TARGET_APPNAME";

    /// <summary>
    /// 添加系统配置。
    /// </summary>
    /// <param name="services">服务集合。</param>
    public static void AddApplication(this IServiceCollection services)
    {
        Console.WriteLine(AppName);
#if DEBUG
        Config.IsDevelopment = true;
        Config.IsDebug = true;
#endif
        services.AddKnown(info =>
        {
            info.Id = AppId;
            info.Name = AppName;
        });
        services.AddModules();

        // 如果仅本地运行，添加本地服务
        services.AddKnownDesktop(option =>
        {
            option.WebRoot = AppContext.BaseDirectory;
            option.ContentRoot = AppContext.BaseDirectory;
            option.Assembly = typeof(AppConfig).Assembly;
            option.Database = db =>
            {
                db.AddSQLite<Microsoft.Data.Sqlite.SqliteFactory>(@"Data Source=.\Sample.db");
                //db.SqlMonitor = c => Console.WriteLine($"{DateTime.Now:HH:mm:ss} {c}");
                //db.OperateMonitors.Add(info => Console.WriteLine(info.ToString()));
            };
        });

        // 如果要请求服务器WebApi，请添加Client
        //services.AddApplicationClient(option =>
        //{
        //    option.BaseAddress = "WebApi根地址";
        //});
    }

    // 添加客户端
    internal static void AddApplicationClient(this IServiceCollection services, Action<ClientOption> action)
    {
        services.AddKnownClient(action);
    }
}